"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : keyword
Case Name   : opengauss关键字operator(非保留)，自定义操作符族
Create At   : 2023-04-23
Owner       : @daiguatutu
Description :
    1.修改参数support_extended_features值为true
    2.创建操作符族
    3.清理环境
Expect      :
    1.修改参数support_extended_features值为true成功
    2.创建操作符族成功
    3.清理环境
History     :
"""

import unittest
import os
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class OperatorTestCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"---{os.path.basename(__file__)} start---")
        self.constant = Constant()
        self.common = Common()
        self.commonsh = CommonSH('PrimaryDbUser')
        self.operator_name = 'Operator_Case0037'
        self.show_msg = self.common.show_param("support_extended_features")

    def test_keyword(self):
        text1 = '---step1: 参数support_extended_features值为true,并重启数据库' \
                ' expect: 修改参数值成功，重启数据库成功---'
        self.log.info(text1)
        result = self.commonsh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'support_extended_features=on')
        self.assertTrue(result, '执行失败' + text1)
        msg = self.commonsh.restart_db_cluster()
        self.log.info(msg)
        self.assertTrue(msg, '执行失败' + text1)

        text2 = '---step2: 创建操作符族; expect: 创建成功---'
        self.log.info(text2)
        sql_cmd = self.commonsh.execut_db_sql(
            f"drop operator family if exists {self.operator_name} "
            f" using btree;"
            f"create operator family {self.operator_name} using btree;")
        self.log.info(sql_cmd)
        self.assertIn("DROP OPERATOR FAMILY", sql_cmd,
                      '执行失败:' + text2)
        self.assertIn("CREATE OPERATOR FAMILY", sql_cmd,
                      '执行失败:' + text2)

    def tearDown(self):
        text3 = '---step3: 清理环境; expect: 清理成功---'
        self.log.info(text3)
        sql_cmd = self.commonsh.execut_db_sql(
            f"drop operator family if exists {self.operator_name} "
            f"using btree;")
        self.log.info(sql_cmd)
        result = self.commonsh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'support_extended_features={self.show_msg}')
        msg = self.commonsh.restart_db_cluster()
        self.log.info(msg)
        self.assertIn("DROP OPERATOR FAMILY", sql_cmd,
                      '执行失败:' + text3)
        self.assertTrue(result, '执行失败' + text3)
        self.assertTrue(msg, '执行失败' + text3)
        self.log.info(f"---{os.path.basename(__file__)} stop---")
